package com.example.shirotest.net;

import com.example.shirotest.entry.Result;
import com.example.shirotest.service.ResultService;
import com.example.shirotest.service.impl.ResultServiceImpl;
import com.example.shirotest.utils.MD5Util;
import org.apache.commons.io.FileUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.regex.Pattern;

@Service
public class Testnet {

    private String urllink = "https://www.gmw.cn/";

    public void findStatic(){
        try {
            URL url = new URL(urllink);
            URLConnection conn = url.openConnection();
            InputStream is = conn.getInputStream();
            System.out.println(conn.getContentEncoding());

            BufferedReader br = new BufferedReader(new InputStreamReader(is, "GBK"));
            String line = null;
            while ((line = br.readLine()) != null){
                System.out.println(line);
            }
            br.close();
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void downloadStaticInfo(){
        try {
            URL url = new URL(urllink);
            File file = new File("D:/javademo/a.html");
            FileOutputStream os = new FileOutputStream(file);
            URLConnection conn = url.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String urlString = "";
            String current;
            while ((current = in.readLine()) != null){
                urlString += current;
            }

            os.write(urlString.getBytes());
            os.close();

            System.out.println("download finished");

        }catch (Exception e){
            e.printStackTrace();
        }
    }


    public void findCss() throws IOException {
        urllink = "https://www.gmw.cn";
        //创建httpclient 实例
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(urllink);

        CloseableHttpResponse response = httpClient.execute(httpGet);
        HttpEntity entity = response.getEntity();

        String content = EntityUtils.toString(entity, "UTF-8");
        //解析网页 得到文档对象
        Document doc = Jsoup.parse(content);
        //获取指定的 <link>
        Elements elements = doc.select("head link");
        URL urlobj = null;
        for (int i = 0; i < elements.size(); i++) {
            Element element = elements.get(i);
            String url = element.attr("href");
            System.out.println(url);

            if(url.matches("^//.*")){
                url = "https:" + url;
            }

            if(!url.matches("^http.*")){
                url = urllink + url;
            }

            urlobj = new URL(url);
            String url2 = urlobj.getProtocol() + "://" + urlobj.getHost() + urlobj.getPath();
            String fileExtension = url2.substring(url.lastIndexOf(".") + 1);

            String encrypt = "";
            try {
                encrypt = MD5Util.encrypt(urlobj.getPath());
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }

            HttpGet picturehttpGet = new HttpGet(url2);
            CloseableHttpResponse pictureResponse = httpClient.execute(picturehttpGet);
            HttpEntity pictureEntity = pictureResponse.getEntity();
            InputStream inputStream = pictureEntity.getContent();

            FileUtils.copyToFile(inputStream,new File("D://javademo//css//" + encrypt + "_" + i + "." + fileExtension ));
            pictureResponse.close();
        }

        response.close();
        httpClient.close();

        System.out.println("css download finished");
    }


    @Autowired
    private ResultService resultService;
    
    public void getHref(){
        //urllink = "https://www.gmw.cn/";  // 1-光明网
        //urllink = "http://www.mod.gov.cn/"; //2-国防部
        //urllink = "http://www.news.cn/world/index.html"; //3-新华网-国际
        //urllink = "https://news.sina.com.cn/"; //4-新浪-国际
        urllink = "https://news.sina.com.cn/"; //5-新浪-国内
        URL urlobj = null;
        String hosturl = "";
        String hostpro = "";
        try {
            urlobj = new URL(urllink);
            hosturl = urlobj.getProtocol() + "://" + urlobj.getHost();
            hostpro = urlobj.getProtocol();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

        try {
            Document document = Jsoup.connect(urllink).get();
            //Elements a = document.getElementsByClass("m_left60").select("a"); // 1
            //Elements a = document.select("div.container:eq(6)").select(".col-md-4-12").select(".tab-bd").select("a"); // 2
            //Elements a = document.select(".guessLike").select("a");  // 3
            //Elements a = document.select("#blk_gjxw_011").select("a"); //4
            Elements a = document.select("#blk_gnxw_011").select("a"); //5-国内

            ArrayList<Result> results = new ArrayList<>();
            for (Element element : a) {
                if(element.text().isEmpty())continue;
                Result result = new Result();
                result.setTitle(element.select("a").text());

                //链接判断，规范化处理
                String href = element.select("a").attr("href");
                if(!href.matches("^http.*")){
                    //相对当前连接的地址
                    if(href.matches("^\\./.*")){
                        href = urllink + href.substring(2);
                    }

                    //以//为开始的地址
                    if(href.matches("^//.*")){
                        href = hostpro + ":" + href;
                    }

                    //相对域名的地址
                    if(href.matches("^/.*")){
                        href = hosturl + href;
                    }
                }

                result.setUrl(href);
                results.add(result);
            }

            if(results.size()>0){
                resultService.saveBatch(results);
            }
            
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        
    }
    

    public void findHref() throws IOException {
        urllink = "https://fzggw.cq.gov.cn/zfxxgk/fdzdgknr/lzyj/";
        String path = "D://javademo/online01.txt";
        try {
            Document document = Jsoup.connect(urllink).get();
            Elements a = document.getElementsByClass("gkm-crul").select("li");

            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(path, true));
            for (Element element : a) {
                if(element.text().isEmpty())continue;
                String title = element.select("a").text();
                String url   = element.select("a").attr("href");
                String date  = element.select(".gkm-cruls3").text();

                //resultService.saveBatch();

                bufferedWriter.write(title + "   "+ url + "   " + date);
                bufferedWriter.newLine();
                //System.out.println(element.attr("href"));
            }
            bufferedWriter.close();


        }catch (IOException e){
            e.printStackTrace();
        }
    }



}
